﻿using Abp.Application.Services;
using Abp.Domain.Repositories;
using Microsoft.AspNetCore.Mvc;
using SportsPlatform.Query.ClientLog;
using SportsPlatform.Application.SportBusiness.ISportBusiness;
using SportsPlatform.Base;
using SportsPlatform.Consts;
using SportsPlatform.Core.Model.SportBusiness;
using SportsPlatform.Model.SportBusiness;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SportsPlatform.Application.Dto.ClientLog;
using SportsPlatform.Application.Query.ClientLog;
using SportsPlatform.WeChat.WeChat;
using Abp.Collections.Extensions;
using SqlSugar;
using Abp.Extensions;

namespace SportsPlatform.Application.SportBusiness.SportBusiness
{
    /// <summary>
    /// 客户端日志记录
    /// </summary>
    [RemoteService(false)]
    public class ClientLogService : SportsPlatformAppServiceBase, IClientLogService
    {
        #region 私有类型
        private readonly IRepository<ClientLog, Guid> _repository;

        #endregion

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="repository"></param>
        public ClientLogService(IRepository<ClientLog, Guid> repository)
        {
            _repository = repository;
        }

        /// <summary>
        /// 新增客户端操作日志
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public async Task<BaseResultDto> AddClientLog(List<AddClientLogQuery> query)
        {
            var res = new BaseResultDto();
            try
            {
                if (query.Count > 0)
                {
                    foreach (var item in query)
                    {
                        var log = new ClientLog();
                        log.Id = Guid.NewGuid();
                        log.NoLocation = item.NoLocation;
                        log.CompetitionProjectName = item.CompetitionProjectName;
                        log.CompetitionProjectId = item.CompetitionProjectId;
                        log.CompetitionName = item.CompetitionName;
                        log.CompetitionId = item.CompetitionId;
                        log.EmployeeId = item.EmployeeId;
                        log.EventName = item.EventName;
                        log.EventsSessionId = item.EventsSessionId;
                        log.Operation= item.Operation;
                        log.OperationUserId = item.OperationUserId;
                        log.OperationTime = item.OperationTime;
                        log.OperationName = item.OperationName;
                        log.GroupName= item.GroupName;
                        log.EmployeeName= item.EmployeeName;
                        var Id = await _repository.InsertAndGetIdAsync(log);
                    }
                    res.IsSuccess = true;
                    res.Message = "操作成功";
                    res.Code = HttpResultCode.Success;
                }
            }
            catch (Exception ex)
            {
                res.IsSuccess = false;
                res.Message = ex.Message;
                res.Code = HttpResultCode.Error;
            }


            return res;
        }

        /// <summary>
        /// 分页获取客户端日志操作记录
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public async Task<PagePagingDto<GetClientLogPageListDto>> GetClientLogPageList(GetClientLogPageListQuery query)
        {
            var res = new PagePagingDto<GetClientLogPageListDto>();
            var clientlog = (await _repository.GetAllListAsync()).WhereIf(!string.IsNullOrWhiteSpace(query.CompetitionName), a => a.CompetitionName.Contains(query.CompetitionName))
                               .WhereIF(!string.IsNullOrWhiteSpace(query.CompetitionProjectName), a => a.CompetitionProjectName.Contains(query.CompetitionProjectName))
                               .WhereIF(!string.IsNullOrWhiteSpace(query.EventName), a => a.EventName.Contains(query.EventName))
                               .WhereIF(!string.IsNullOrWhiteSpace(query.GroupName), a => a.GroupName.Contains(query.GroupName))
                               .WhereIF(!string.IsNullOrWhiteSpace(query.Operation), a => a.Operation.Contains(query.Operation))
                               .WhereIF(!string.IsNullOrWhiteSpace(query.OperationName), a => a.OperationName.Contains(query.OperationName))
                               .WhereIF(query.OperationBeginTime.HasValue, a => a.OperationTime >= query.OperationBeginTime.Value)
                               .WhereIF(query.OperationEndTime.HasValue, a => a.OperationTime < query.OperationEndTime.Value.AddDays(1)).ToList();//结束时间要包含到23时59分59秒
            var log = clientlog.Skip((query.PageIndex - 1) * query.PageSize).Take(query.PageSize).ToList();
            var list = new List<GetClientLogPageListDto>();
            if (log.Count() > 0)
            {
                foreach (var item in log)
                {
                    var dto = new GetClientLogPageListDto();
                    dto.Id = item.Id;
                    dto.CompetitionName = item.CompetitionName;
                    dto.CompetitionProjectName = item.CompetitionProjectName;
                    dto.EventName = item.EventName;
                    dto.GroupName = item.GroupName;
                    dto.NoLocation = item.NoLocation.ToString();
                    dto.EmployeeName = item.EmployeeName;
                    dto.Operation = item.Operation;
                    dto.OperationName = item.OperationName;
                    dto.OperationTime = item.OperationTime.ToString("yyyy-MM-dd HH:mm:ss");
                    list.Add(dto);
                }
                res.Code = HttpResultCode.Success;
                res.Data = list;
                res.IsSuccess = true;
                res.TotalCount = clientlog.Count();
                res.Message = "查询成功";
            }
            else
            {
                res.Code = HttpResultCode.NotData;
                res.IsSuccess = false;
                res.Message = "未查询到数据";
            }
            return res;
        }
    }
}
